package com.example.dilidili_ting;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.dilidili_ting.bean.MovieBeanTing;
import com.example.dilidili_ting.view.HomeViewTing;
import com.example.dilidili_ting.view.LikeViewTing;
import com.example.dilidili_ting.view.MyInfoViewTing;
import com.ryan.rv_gallery.GalleryRecyclerView;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

import nl.joery.animatedbottombar.AnimatedBottomBar;

public class MainActivity extends AppCompatActivity {

    private FrameLayout mainBodyTing;
    private AnimatedBottomBar bottomBarTing;
    private HomeViewTing homeViewTing;
    private MyInfoViewTing myInfoViewTing;
    private LikeViewTing likeViewTing;
    private View mCurrentViewTing;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        initView();
        createView(0);
    }

    private void initView() {
        mainBodyTing = (FrameLayout) findViewById(R.id.main_body_ting);
        bottomBarTing = (AnimatedBottomBar) findViewById(R.id.bottom_bar_ting);
        bottomBarTing.setOnTabSelectListener(new AnimatedBottomBar.OnTabSelectListener() {

            @Override
            public void onTabSelected(int i, @Nullable AnimatedBottomBar.Tab tab, int i1, @NotNull AnimatedBottomBar.Tab tab1) {
                removeAllView();
                createView(i1);
            }

            @Override
            public void onTabReselected(int i, @NotNull AnimatedBottomBar.Tab tab) {}
        });
    }

    /**
     登录成功,设置退出登录的回调方法
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //判断回传数据是否为空
        if (data!=null){
            boolean isLogin=data.getBooleanExtra("isLogin",false); //判断回传数据中的登录状态是否已登录
            if (isLogin){
                createView(0);//显示首页页面
            }
            if (myInfoViewTing!=null) //判断我的页面是否存在，如果存在则更新用户名显示内容
            {
                myInfoViewTing.loginStatus();
            }
            if (likeViewTing!=null) //判断收藏页面是否存在，如果存在则更新用户内容
            {
                likeViewTing.loginStatus();
            }

        }
    }

    /**
     移除所有的视图
     */
    private void removeAllView(){
        for (int i =0; i<mainBodyTing.getChildCount();i++){
            mainBodyTing.getChildAt(i).setVisibility(View.GONE);
//            mainBodyTing.removeView(mainBodyTing.getChildAt(i));
        }
    }

    /**
     创建显示视图
     */
    private void createView(int viewIndex){
        switch (viewIndex){
            case 0:
                if(homeViewTing==null){
                    homeViewTing=new HomeViewTing(MainActivity.this);
                    mainBodyTing.addView(homeViewTing.getView());
                } else {
                    homeViewTing.getView();
                }
                //将获取的参数传给首页页面
                homeViewTing.showView();
                break;
            case 1:
                if(likeViewTing==null){
                    likeViewTing=new LikeViewTing(MainActivity.this);
                    mainBodyTing.addView(likeViewTing.getView());
                } else {
                    likeViewTing.getView();
                }
                //将获取的参数传给收藏页面
                likeViewTing.loginStatus();
                likeViewTing.showView();
                break;
            case 2:
                if(myInfoViewTing==null){
                    myInfoViewTing=new MyInfoViewTing(MainActivity.this);
                    mainBodyTing.addView(myInfoViewTing.getView());
                } else {
                    myInfoViewTing.getView();
                }
                //将获取的参数传给我的页面
                myInfoViewTing.showView();
                break;
        }
    }
}